package com.csun.dsms.service.impl;

import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.DsmsRecordMapper;
import com.csun.dsms.model.domain.DsmsRecord;
import com.csun.dsms.model.vo.ServiceRecordVo;
import com.csun.dsms.model.vo.poor.PoorServiceVo;
import com.csun.dsms.model.vo.poor.PoorVo;
import com.csun.dsms.model.vo.project.ProjectVo;
import com.csun.dsms.service.DsmsPoorService;
import com.csun.dsms.service.DsmsServiceRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class DsmsServiceRecordServiceImpl extends BaseService<DsmsRecord> implements DsmsServiceRecordService {

    @Resource
    private DsmsRecordMapper dsmsRecordMapper;
    @Resource
    private DsmsPoorService dsmsPoorServiceImpl;

    /*
     *特困人员服务进度查询
     * */
    @Override
    public List<ServiceRecordVo> serviceRecordQuery(ServiceRecordVo serviceRecordVo) {
        return dsmsRecordMapper.serviceRecordQuery(serviceRecordVo);
    }

    /*
     * 查询特困人员该月份的服务项目
     * */
    @Override
    public PoorVo queryServiceProject(ServiceRecordVo serviceRecordVo) {
        List<ProjectVo> projectVos = dsmsRecordMapper.queryServiceProject(serviceRecordVo);
        PoorVo poorVo = dsmsPoorServiceImpl.showById(serviceRecordVo.getPoorId());
        if(poorVo == null){
            return null;
        }
        poorVo.setAreaName(serviceRecordVo.getAreaName());
        poorVo.setMonth(serviceRecordVo.getServiceTime());
        Set<String> projects = new HashSet<>();
        List<ProjectVo> poorServiceVos = new ArrayList<>();
        ProjectVo pv = new ProjectVo();
        Integer completedService = 0;
        if(projectVos.size()>0){
            for (ProjectVo projectVo:projectVos) {
                projects.add(projectVo.getProjectName());
            }
            for (String s:projects) {
                for (ProjectVo p:projectVos) {
                    if(p.getProjectName().equals(s)){
                        completedService++;
                    }
                }
                pv.setProjectName(s);
                pv.setCount(completedService);
                poorServiceVos.add(pv);
                pv = new ProjectVo();
                completedService = 0;
            }
        }
        poorVo.setProjectVos(poorServiceVos);
        //服务项目去重
        /*for (ServiceRecordVo s:serviceRecordVos) {
            projects.add(s.getProjectName());
        }
        if(projects.size()!=0){
             for (String s:projects){
                 for (ServiceRecordVo sr:serviceRecordVos){
                    if(sr.getProjectName().equals(s)){
                        allService+=1;
                    }
                    if (sr.getProjectName().equals(s)&&sr.getStatus()==2){
                        completedService+=1;
                    }
                }
                PoorServiceVo poorServiceVo = new PoorServiceVo();
                poorServiceVo.setProject(s);
                poorServiceVo.setAllService(allService);
                poorServiceVo.setCompletedService(completedService);
                DecimalFormat df = new DecimalFormat("0.00%");
                String format = df.format((completedService/Double.valueOf(allService)));
                poorServiceVo.setPercent(format);
                poorServiceVos.add(poorServiceVo);
                poorServiceVo = new PoorServiceVo();
                allService=0;
                completedService=0;
            }
            poorVo.setPoorServiceVos(poorServiceVos);
        }*/
        return poorVo;
    }
    /*
     * 网格月服务进度查询
     * */
    @Override
    public List<ServiceRecordVo> serviceRecordQueryByArea(ServiceRecordVo serviceRecordVo) {
        return dsmsRecordMapper.serviceRecordQueryByArea(serviceRecordVo);
    }
    /*
     * 网格日服务进度查询
     * */
    @Override
    public List<ServiceRecordVo> serviceRecordQueryByDay(ServiceRecordVo serviceRecordVo) {
        return dsmsRecordMapper.serviceRecordQueryByDay(serviceRecordVo);
    }
    /*
     * 查询服务项目是否分配
     * */
    @Override
    public List<ServiceRecordVo> serviceProjectQuery(ServiceRecordVo serviceRecordVo) {
        return dsmsRecordMapper.serviceProjectQuery(serviceRecordVo);
    }

    /*
     * 查询服务项目占比
     * */
    @Override
    public List<ServiceRecordVo> serviceQueryByProject() {
        List<ServiceRecordVo> serviceRecordVos = dsmsRecordMapper.serviceQueryByProject();
        //总服务次数
       /* Integer service = 0;
        for (ServiceRecordVo s:serviceRecordVos) {
            service += s.getCompletedService();
        }*/
        Integer service = serviceRecordVos.size();
        if(service == 0){
            return serviceRecordVos;
        }
        DecimalFormat df = new DecimalFormat("0.00%");
        for (ServiceRecordVo s:serviceRecordVos) {
            s.setPercent(df.format(s.getCompletedService()/Double.valueOf(service)));
        }
        return serviceRecordVos;
    }

    //月服务进度详情查看
    @Override
    public List<ProjectVo> serviceRecordInfoByArea(ServiceRecordVo serviceRecordVo) {
        //查询片区当月服务项目
        List<ProjectVo> serviceRecordVos = dsmsRecordMapper.serviceRecordInfoByArea(serviceRecordVo);
        Set<String> project = new HashSet<>();
        for (ProjectVo s:serviceRecordVos) {
            project.add(s.getProjectName());
        }
        List<ProjectVo> projectVos = new ArrayList<>();
        ProjectVo projectVo = new ProjectVo();
        Integer completedService = 0;
        for (String s:project) {
            for (ProjectVo p:serviceRecordVos) {
                if(p.getProjectName().equals(s)){
                    completedService++;
                }
            }
            projectVo.setProjectName(s);
            projectVo.setCount(completedService);
            projectVos.add(projectVo);
            projectVo = new ProjectVo();
            completedService = 0;
        }
        /*List<ServiceRecordVo> serviceRecordVoList = new ArrayList<>();
        for (String s:project) {
            ServiceRecordVo sr = new ServiceRecordVo();
            Integer allService = 0;
            Integer completedService = 0;
            String format = null;
            for (ServiceRecordVo serviceRecord:serviceRecordVos) {
                if(serviceRecord.getProjectName().equals(s)){
                    allService++;
                }
                if(serviceRecord.getProjectName().equals(s) && serviceRecord.getStatus() == 2){
                    completedService++;
                }
                DecimalFormat df = new DecimalFormat("0.00%");
                format = df.format(completedService / Double.valueOf(allService));
            }
            sr.setProjectName(s);
            sr.setAllService(allService);
            sr.setCompletedService(completedService);
            sr.setPercent(format);
            serviceRecordVoList.add(sr);
        }*/
        return projectVos;
    }

    //日服务进度详情查询
    @Override
    public List<ServiceRecordVo> serviceRecordInfoByDay(ServiceRecordVo serviceRecordVo) {
        return dsmsRecordMapper.serviceRecordInfoByDay(serviceRecordVo);
    }
}
